<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Order_model extends CI_Model {

	public $variable;

	public function __construct()
	{
		parent::__construct();
		
	}

	//得到所有鱼的种类
	public function getFish(){
		return $this->db->where(array('type'=>0))->get('bank')->result_array();
	}

	//得到订单状态
	public function getOrderStatus($where){
		return $this->db->where($where)->get('order')->row_array();
	}

	//得到一个订单操作详情
	public function getOneOrderList($where){
		if($where['bid']){
			return $this->db->where($where)->get('orderlog')->result_array();
		}else{
			return $this->db->where($where)->get('orderlog')->row_array();
		}
		
	}

	//得到所有的订单
	public function getAllOrder($where,$page,$limit){
		return $this->db->select("o.id,b.bankname,o.price,o.number,o.amount,o.actual,o.time,o.status,o.payment,o.consumer,o.regulars,o.loss,o.memo")->from("order as o")->join("bank as b",'o.bandid=b.id')->where($where)->order_by('o.status',0)->order_by('o.id','desc')->order_by('o.status',1)->limit($limit,($page-1)*$limit)->get()->result_array();
	}

	public function getAllOrderNum($where){
		return $this->db->where($where)->count_all_results('order');
	}

	//获取所有为审核的订单
	// public function getAllNoReviewOrder($where,$page,$limit){
	// 	return $this->db->where($where)->limit($limit,($page-1)*$limit)->get('order')->result_array();
	// }


	//得到一天的进货订单的列表
	public function getOneDayBuyOrder($where){
		return $this->db->select('br.*,b.bankname,a.username,bs.account')->from('buyorder as br')->join('bank as b','br.bandid=b.id')->join('admin as a','br.operator=a.id')->join('business as bs','bs.id=br.bsid')->where($where)->get()->result_array();
	}

	//获取进货的总额
	public function getAllBuyOrderData($where){
		return $this->db->select("sum(amount) as total,sum(number) as num,sum(lossnum) as lonum,sum(loss) as lo")->where($where)->get('buyorder')->row_array();
	}

	//进货添加
	public function addBuyOrder($data){
		$this->db->trans_begin();
		$this->db->insert('buyorder', $data);
		$data['bid'] = $this->db->insert_id();
		$data['operationmemo'] = "添加订单";
		unset($data['reviewed']);
		$this->db->insert('buyorderlog', $data);
		if ($this->db->trans_status() === FALSE)
		{
		    $this->db->trans_rollback();
		    return FALSE;
		}
		else
		{
		    $this->db->trans_commit();
		    return TRUE;
		}
	}

	//常用客户名字转换
	public function intToStr($where){
		return $this->db->select('account')->where($where)->get('agent')->row_array();
	}

	public function getOneDayOrderNum($where){
		return $this->db->where($where)->count_all_results('order');
	}

	//获取当日销售订单列表
	public function getOneDayOrder($where,$page,$limit){
		return $this->db->select("o.id,b.bankname,o.price,o.number,o.amount,o.actual,o.time,o.status,o.payment,o.consumer,o.regulars,o.loss,o.memo,o.lossnum")->from("order as o")->join("bank as b",'o.bandid=b.id')->where($where)->order_by('o.status',0)->order_by('o.id','desc')->order_by('o.status',1)->limit($limit,($page-1)*$limit)->get()->result_array();
	}

	//销售订单添加
	public function addOrder($data){
		$this->db->trans_begin();
		$this->db->insert('order', $data);
		$data['bid'] = $this->db->insert_id();
		$data['operationmemo'] = "添加订单";
		unset($data['reviewed']);
		$this->db->insert('orderlog', $data);
		if ($this->db->trans_status() === FALSE)
		{
		    $this->db->trans_rollback();
		    return FALSE;
		}
		else
		{
		    $this->db->trans_commit();
		    return TRUE;
		}
	}

	//更新订单
	public function editOrderStatus($where,$data,$operationmemo){
		$this->db->trans_begin();
		$data1 = $this->db->where($where)->get('order')->row_array();
		$data['operator'] = $_SESSION['id'];
		$data['time'] = date("Y-m-d H:i:s",time());
		if($data['status'] == 3){
			$data['actual'] = $data1['amount']-$data['loss'];
			$data['number'] = $data1['number']-$data['lossnum'];
			$data['payment'] = 0;
		}else if($data['status'] == 4){
			$data['enable'] = 1;
		}
		$this->db->where($where)->update('order', $data);
		$da = $this->db->where($where)->get('order')->row_array();

		$da['operationmemo'] = $operationmemo;
		$da['time'] = date("Y-m-d H:i:s",time());
		$da['bid'] = $da['id'];
		//$da['operator'] = $_SESSION['id'];
		unset($da['reviewed']);
		unset($da['id']);
		$this->db->insert('orderlog', $da);
		if ($this->db->trans_status() === FALSE)
		{
		    $this->db->trans_rollback();
		    return FALSE;
		}
		else
		{
		    $this->db->trans_commit();
		    return TRUE;
		}
	}

	//回滚到指定数据
	public function undoData($data){
		$this->db->trans_begin();
		$da = $this->getOneOrderList(array('bid'=>$data['bid']));
		foreach ($da as $k => $v) {
			if($v['id'] > $data['id']){
				$this->db->delete('orderlog', array('id'=>$v['id']));
			}
		}
		$this->db->update('order', $data);
		if ($this->db->trans_status() === FALSE)
		{
		    $this->db->trans_rollback();
		    return FALSE;
		}
		else
		{
		    $this->db->trans_commit();
		    return TRUE;
		}
	}

	//审核通过
	public function approved($where,$data){
		$this->db->where($where)->update('order', $data);
	}

	//修改订单内容
	public function editOrderHandle($where,$data,$operationmemo){
		$this->db->trans_begin();
		$data1 = $this->db->where($where)->get('order')->row_array();
		$data['operator'] = $_SESSION['id'];
		$data['time'] = date("Y-m-d H:i:s",time());
		$this->db->where($where)->update('order', $data);
		$da = $this->db->where($where)->get('order')->row_array();
		$da['operationmemo'] = $operationmemo;
		$da['time'] = date("Y-m-d H:i:s",time());
		$da['bid'] = $da['id'];
		unset($da['reviewed']);
		unset($da['id']);
		$this->db->insert('orderlog', $da);
		if ($this->db->trans_status() === FALSE)
		{
		    $this->db->trans_rollback();
		    return FALSE;
		}
		else
		{
		    $this->db->trans_commit();
		    return TRUE;
		}
	}	

	//得到某一种鱼的所有数量销量
	public function getAllSpeciesNum($where){
		return $this->db->select("b.bankname,sum(0+o.number) as num")->from('order as o')->join("bank as b",'o.bandid=b.id')->where($where)->get()->row_array();
	}

	//得到所有的销售数据
	public function getSalesOrder($where){
		return $this->db->where($where)->get('order')->result_array();
	}
	
}

/* End of file order_model.php */
/* Location: ./application/models/order_model.php */